import 'package:aqua_plunge/constants/constants.dart';
import 'package:aqua_plunge/dao/purchase_dao.dart';
import 'package:aqua_plunge/http/app_manager.dart';
import 'package:aqua_plunge/util/MedalType.dart';
import 'package:aqua_plunge/util/toast_util.dart';
import 'package:aqua_plunge/widget/app_bar.dart';
import 'package:flutter/material.dart';

import '../constants/colors.dart';
import '../widget/MedalsDialog.dart';

class MedalStorePage extends StatefulWidget {
  const MedalStorePage({super.key});

  @override
  State<MedalStorePage> createState() => _MedalStorePageState();
}

class _MedalStorePageState extends State<MedalStorePage> {
  get _contentView => Container(
        padding: const EdgeInsets.fromLTRB(44, 20, 44, 20),
        decoration: const BoxDecoration(
            image: DecorationImage(
                image: AssetImage('images/img_primary_bg.png'),
                fit: BoxFit.cover)),
        child: ListView(
          children: [
            Container(
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(14),
                  color: AppColors.textbgColor),
              height: 30,
              alignment: Alignment.center,
              child: const Text(
                Constants.upgrade,
                style: TextStyle(color: Colors.white, fontSize: 12),
              ),
            ),
            const SizedBox(
              height: 42,
            ),
            _medal1('images/icon_level1.png', 'images/icon_super_level1.png'),
            Visibility(
                visible: !AppManager.isAcquireMedal(MedalType.upgrade1),
                child: _wallet(55, () {
                  _buyMedal(MedalType.upgrade1, 55);
                })),
            const SizedBox(
              height: 20,
            ),
            _medal1('images/icon_level2.png', 'images/icon_super_level2.png'),
            Visibility(
                visible: !AppManager.isAcquireMedal(MedalType.upgrade2),
                child: _wallet(65, () {
                  if (AppManager.isAcquireMedal(MedalType.level2)) {
                    _buyMedal(MedalType.upgrade2, 65);
                  } else {
                    appToast.showTipLong(
                        'You must first go to get the initial medal');
                  }
                })),
            const SizedBox(
              height: 20,
            ),
            _medal1('images/icon_level3.png', 'images/icon_super_level3.png'),
            Visibility(
                visible: !AppManager.isAcquireMedal(MedalType.upgrade3),
                child: _wallet(75, () {
                  if (AppManager.isAcquireMedal(MedalType.level3)) {
                    _buyMedal(MedalType.upgrade3, 75);
                  } else {
                    appToast.showTipLong(
                        'You must first go to get the initial medal');
                  }
                })),
            const SizedBox(
              height: 20,
            ),
            _medal1('images/icon_level4.png', 'images/icon_super_level4.png'),
            Visibility(
                visible: !AppManager.isAcquireMedal(MedalType.upgrade4),
                child: _wallet(85, () {
                  if (AppManager.isAcquireMedal(MedalType.level4)) {
                    _buyMedal(MedalType.upgrade4, 85);
                  } else {
                    appToast.showTipLong(
                        'You must first go to get the initial medal');
                  }
                })),
          ],
        ),
      );

  _medal1(String value1, String value2) {
    return SizedBox(
      height: 100,
      width: double.infinity,
      child: Row(
        children: [
          Image.asset(
            value1,
            width: 72,
            height: 82,
          ),
          const SizedBox(
            width: 36,
          ),
          Image.asset(
            'images/icon_arrow_right.png',
            width: 40,
            height: 40,
          ),
          Expanded(
            child: Image.asset(value2, fit: BoxFit.cover),
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBar("Medal Store"),
      body: _contentView,
    );
  }

  _wallet(int value, Function function) {
    return GestureDetector(
      onTap: () {
        showDialog(
            context: context,
            builder: (context) {
              return AlertDialog(
                title: const Text('Are you sure to upgrade?'),
                actions: [
                  TextButton(
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                      child: const Text('Return')),
                  TextButton(
                      onPressed: () {
                        function();
                        Navigator.of(context).pop();
                      },
                      child: const Text('Yes'))
                ],
              );
            });
      },
      child: Container(
        margin: const EdgeInsets.only(top: 5, right: 45),
        width: double.infinity,
        alignment: Alignment.centerRight,
        child: Container(
          width: 69,
          height: 32,
          decoration: const BoxDecoration(
              image:
                  DecorationImage(image: AssetImage('images/icon_wallet.png'))),
          child: Container(
            alignment: Alignment.center,
            padding: const EdgeInsets.only(left: 25),
            child: Text(
              "$value",
              style: const TextStyle(
                  color: AppColors.primaryColor,
                  fontSize: 16,
                  fontWeight: FontWeight.bold),
            ),
          ),
        ),
      ),
    );
  }

  void _buyMedal(MedalType type, int value) {
    PurchaseDao.reviewModelConsume(value, () {
      AppManager.getMedal(type);
      setState(() {});
      _checkShowDialog();
    }, (error) {
      appToast.showTip(error);
    });
  }

  _checkShowDialog(){
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      if(AppManager.isMedalFirstLaunch(MedalType.upgrade1)??false){
        _showWelcomeDialog(MedalType.upgrade1);
      }
      if(AppManager.isMedalFirstLaunch(MedalType.upgrade2)??false){
        _showWelcomeDialog(MedalType.upgrade2);
      }
      if(AppManager.isMedalFirstLaunch(MedalType.upgrade3)??false){
        _showWelcomeDialog(MedalType.upgrade3);
      }
      if(AppManager.isMedalFirstLaunch(MedalType.upgrade4)??false){
        _showWelcomeDialog(MedalType.upgrade4);
      }

    });
  }

  void _showWelcomeDialog(MedalType mtype) {
    showDialog(
      context: context,
      builder: (context) {
        return MedalsDialog(type: mtype);
      },
    );
  }
}
